package study.itwill.common;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.apache.log4j.Logger;

public class Util {
	
	static Logger logger = Logger.getLogger(Util.class);
	
	/** 두 개의 정수 사이에서 난수를 리턴하는 메서드 */
	public static int random(int min, int max) {
		
		logger.debug("=========== method start >> random");
		
		int num = (int)((Math.random() * (max - min + 1)) + min);
		
		logger.debug("=========== method end >> random");
		
		return num;
	}
	
	/** HTML 태그를 제거하는 정규표현식 */
	public static String removeTag(String content) {
		
		logger.debug("=========== method start >> removeTag");
		
		Pattern SCRIPTS = Pattern.compile("<(no)?script[^>]*>.*?</(no)?script>",Pattern.DOTALL);
		Pattern STYLE = Pattern.compile("<style[^>]*>.*</style>",Pattern.DOTALL);
		Pattern TAGS = Pattern.compile("<(\"[^\"]*\"|\'[^\']*\'|[^\'\">])*>");
		//Pattern nTAGS = Pattern.compile("<\\w+\\s+[^<]*\\s*>");
		Pattern ENTITY_REFS = Pattern.compile("&[^;]+;");
		Pattern WHITESPACE = Pattern.compile("\\s\\s+");
			
		Matcher m;
			
		m = SCRIPTS.matcher(content);
		content = m.replaceAll("");
		m = STYLE.matcher(content);
		content = m.replaceAll("");
		m = TAGS.matcher(content);
		content = m.replaceAll("");
		m = ENTITY_REFS.matcher(content);
		content = m.replaceAll("");
		m = WHITESPACE.matcher(content);
		content = m.replaceAll(" "); 
		
		logger.debug("=========== method end >> removeTag");

		return content;
	}
}
